package com.xmy.cultivate.characteristic;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.xmy.cultivate.entity.FincType;
import com.xmy.cultivate.entity.FincTypeScope;
import com.xmy.cultivate.service.IFincTypeScopeService;
import com.xmy.cultivate.service.IFincTypeService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.stream.Collectors;

@Service
public class Characteristic {

    @Autowired
    IFincTypeService iFincTypeService;
    @Autowired
    IFincTypeScopeService iFincTypeScopeService;



    public IPage<FincType> findAll(Map<String, String> reMap) {
        int pageNum = StringUtils.isEmpty(reMap.get("pageNum")) ? 1 : Integer.parseInt(reMap.get("pageNum"));
        int pageSize = StringUtils.isEmpty(reMap.get("pageSize")) ? 10 : Integer.parseInt(reMap.get("pageSize"));
        IPage<FincType> iPage = new Page<>(pageNum, pageSize);
        QueryWrapper<FincType> queryWrapper = new QueryWrapper<>();

        queryWrapper.eq("pid", 0);
        iFincTypeService.page(iPage, queryWrapper);
        if (iPage.getRecords().isEmpty()) {
            return iPage;
        }

        Set<Long> parentIds = iPage.getRecords().stream().map(FincType::getId).collect(Collectors.toSet());
        List<FincType> allChildren = new ArrayList<>();

        while (!parentIds.isEmpty()) {
            QueryWrapper<FincType> childWrapper = new QueryWrapper<>();
            childWrapper.in("pid", parentIds);
            List<FincType> children = iFincTypeService.list(childWrapper);
            allChildren.addAll(children);
            parentIds = children.stream().map(FincType::getId).collect(Collectors.toSet());
        }

        List<FincType> allChapters = new ArrayList<>();
        allChapters.addAll(iPage.getRecords());
        allChapters.addAll(allChildren);
        Map<Long, List<FincType>> chapterMap = allChapters.stream().collect(Collectors.groupingBy(FincType::getPid));

        buildTree(iPage.getRecords(), chapterMap);

        // 查询每个 FincType 的 FincTypeScope 列表
//        for (FincType fincType : allChapters) {
//            List<FincTypeScope> scopeList = iFincTypeScopeService.list(new QueryWrapper<FincTypeScope>().eq("finc_type_id", fincType.getId()));
//            fincType.setFincTypeScopeList(scopeList);
//        }

        return iPage;
    }

    private void buildTree(List<FincType> chapters, Map<Long, List<FincType>> chapterMap) {
        for (FincType chapter : chapters) {
            List<FincType> children = chapterMap.getOrDefault(chapter.getId(), new ArrayList<>());
            chapter.setChildren(children);
            buildTree(children, chapterMap);
        }
    }
}
